package com.platform.apex.util;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.platform.apex.cma.config.TypesafeConfig;

public class MetaDataUtil {

	public static final Logger logger = LoggerFactory.getLogger(MetaDataUtil.class);

	private static boolean isDBInit = false;
	private static Map<String, String> jdbcConfig = new HashMap<String, String>();
	private static SQLMetadataConnector metadataConnector;
	private final static ObjectMapper OBJECT_MAPPER = new ObjectMapper();
	public static final String CONF_DATAVIPLATFORM_BASE_JDKB_URL = "dataViPlatform.core.jdbc.url";
	public static final String CONF_DATAVIPLATFORM_BASE_JDKB_DRIVERCLS = "dataViPlatform.core.jdbc.driverCls";
	public static final String CONF_DATAVIPLATFORM_BASE_JDKB_USER = "dataViPlatform.core.jdbc.user";
	public static final String CONF_DATAVIPLATFORM_BASE_JDKB_PASSWORD = "dataViPlatform.core.jdbc.password";
	public static void initDBConnector() {
		if (!isDBInit ) {
//			String jdbcUrl = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_URL);
//			String driverCls = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_DRIVERCLS);
//			String jdbcUser = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_USER);
//			String jdbcPassword = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_PASSWORD);
			
			String jdbcUrl = "jdbc:mysql://10.0.3.192:3306/eason?useUnicode=true&characterEncoding=UTF-8";
			String driverCls = "com.mysql.jdbc.Driver";
			String jdbcUser = "root";
			String jdbcPassword = "Tjsj";

			jdbcConfig.put("url", jdbcUrl);
			jdbcConfig.put("driver_class", driverCls);
			jdbcConfig.put("user", jdbcUser);
			jdbcConfig.put("password", jdbcPassword);

			logger.info("JdbcConfig:\t" + jdbcConfig.toString());

			isDBInit = true;
		}
	}

	
	public static void initDBConnector(String jdbcUrl,String driverCls,String jdbcUser,String jdbcPassword) {
		if (!isDBInit ) {
//			String jdbcUrl = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_URL);
//			String driverCls = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_DRIVERCLS);
//			String jdbcUser = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_USER);
//			String jdbcPassword = typesafeConfig.getString(CONF_DATAVIPLATFORM_BASE_JDKB_PASSWORD);
			
//			String jdbcUrl = "jdbc:mysql://10.0.3.192:3306/eason?useUnicode=true&characterEncoding=UTF-8";
//			String driverCls = "com.mysql.jdbc.Driver";
//			String jdbcUser = "root";
//			String jdbcPassword = "Tjsj";

			jdbcUrl = jdbcUrl + "?useUnicode=true&characterEncoding=UTF-8";
			jdbcConfig.put("url", jdbcUrl);
			jdbcConfig.put("driver_class", driverCls);
			jdbcConfig.put("user", jdbcUser);
			jdbcConfig.put("password", jdbcPassword);

			logger.info("JdbcConfig:\t" + jdbcConfig.toString());

			isDBInit = true;
		}
	}
	/*
	 * Get db connection from config file
	 */
	public static SQLMetadataConnector getMetadataConnector() throws Exception {

		if (!isDBInit) {
			logger.error("DBConnector not init.");
			//throw new Exception("DBConnector not init.");
		}

		if (metadataConnector == null) {
			ObjectMapper jsonMapper = new ObjectMapper();

			HashMap<String, Object> dbMap = new HashMap<String, Object>();
			dbMap.put("createTables", "true");
			dbMap.put("connectURI", jdbcConfig.get("url"));
			dbMap.put("user", jdbcConfig.get("user"));
			dbMap.put("password", jdbcConfig.get("password"));

			String dbJson = jsonMapper.writeValueAsString(dbMap);
			MetadataStorageConnectorConfig connectorConfig = jsonMapper.readValue(dbJson,
					MetadataStorageConnectorConfig.class);

			List<String> usedTableList = new ArrayList<String>();
			MetadataStorageTablesConfig tableConfig = new MetadataStorageTablesConfig(usedTableList);
			metadataConnector = new MySQLConnector(connectorConfig, tableConfig);
		}
		return metadataConnector;
	}

	public static Map<String, Object> getMapByResultSet(ResultSet r) {

		Map<String, Object> mapInfo = new HashMap<String, Object>();

		try {
			ResultSetMetaData rmd = r.getMetaData();
			int columCount = rmd.getColumnCount();

			for (int iCounter = 1; iCounter <= columCount; iCounter++) {
				String colName = rmd.getColumnName(iCounter);
				mapInfo.put(colName, r.getObject(iCounter));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("MetaDataUtil, getMapByResultSet error:", ex);
		}

		if (mapInfo.size() > 0) {
			return mapInfo;
		} else {
			return null;
		}
	}

	public static Map<String, Object> getMapByResultSetLabel(ResultSet r) {

		Map<String, Object> mapInfo = new HashMap<String, Object>();

		try {

			ResultSetMetaData rmd = r.getMetaData();

			int columCount = rmd.getColumnCount();

			for (int iCounter = 1; iCounter <= columCount; iCounter++) {
				String colName = rmd.getColumnLabel(iCounter);
				mapInfo.put(colName, r.getObject(iCounter));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.error("MetaDataUtil, getMapByResultSetLabel error:", ex);
		}

		if (mapInfo.size() > 0) {
			return mapInfo;
		} else {
			return null;
		}
	}

	public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {

		if (map == null) {
			return null;
		}
		return OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(map), beanClass);

	}

	public static Map<String, Object> objectToMap(Object obj) throws Exception {

		if (obj == null) {
			return null;
		}
		return OBJECT_MAPPER.readValue(OBJECT_MAPPER.writeValueAsString(obj), HashMap.class);
	}
	
	public static String getUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
}
